﻿#region namespaces

using System;
using System.Drawing;
using System.Web.UI.WebControls;

using SLIITPro.Data.Entity;
using SLIITPro.Data.Extension;

#endregion

namespace SLIITPro.SysUsers
{
    public partial class UserAdministrations : System.Web.UI.Page
    {
        private const string RemoveTypeFilter = "<a href='?UserType=' alt='{0} {1}'>{0} {1}</a>";
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected string GetLinkText(bool id)
        {
            return id ? "Deavtivate" : "Activate";
        }

        protected Color GetLinkColor(bool id)
        {
            return id ? Color.Red : Color.Green;
        }
        protected void GoToPage_TextChanged(object sender, EventArgs e)
        {
            var txtGoToPage = (TextBox)sender;

            int pageNumber;
            if (int.TryParse(txtGoToPage.Text.Trim(), out pageNumber) && pageNumber > 0 && pageNumber <= this.grdUsers.PageCount)
            {
                grdUsers.PageIndex = pageNumber - 1;
            }
            else
            {
                grdUsers.PageIndex = 0;
            }
        }

        protected void ddl_SelectedIndexChanged(object sender, EventArgs e)
        {
            var dropDown = (DropDownList)sender;
            grdUsers.PageSize = int.Parse(dropDown.SelectedValue);
        }

        protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            var gridView = (GridView)sender;
            switch (e.Row.RowType)
            {
                case DataControlRowType.Pager:
                    {
                        var lblTotalNumberOfPages = (Label)e.Row.FindControl("lblTotalNumberOfPages");
                        lblTotalNumberOfPages.Text = gridView.PageCount.ToString();

                        var txtGoToPage = (TextBox)e.Row.FindControl("txtGoToPage");
                        txtGoToPage.Text = (gridView.PageIndex + 1).ToString();

                        var ddlPageSize = (DropDownList)e.Row.FindControl("ddlPageSize");
                        ddlPageSize.SelectedValue = gridView.PageSize.ToString();
                    }
                    break;

                case DataControlRowType.Footer:
                    {
                        if (!string.IsNullOrEmpty(Request.QueryString["UserType"]))
                        {
                            { e.Row.Cells[8].Text = string.Format(RemoveTypeFilter, "Remove", "Filter"); }
                        }
                    }
                    break;
            }
        }

        private UserExtension _userExtension;
        private Users _users;
        protected void btnBan_Click(object sender, EventArgs e)
        {
            lblSMessage.Text = string.Empty;
            lblSMessage.Text = string.Empty;
            _userExtension = new UserExtension();
            foreach (GridViewRow row in grdUsers.Rows)
            {
                try
                {
                    var chkbx = (CheckBox)row.FindControl("chkSelect");
                    if (chkbx != null && chkbx.Checked)
                    {
                        _users = new Users(_userExtension.SelectUser("UserId", grdUsers.DataKeys[row.RowIndex].Value.ToString()).Rows[0]);
                        if (Page.User.Identity.Name.Equals(_users.Email, StringComparison.OrdinalIgnoreCase))
                        {
                            lblSMessage.Text = string.Format("<b>User {0} {1} is Administrator</b><br/>You cannot change activation mode of your own account<br/>",
                                                             _users.FirstName, _users.LastName);
                        }
                        else
                        {

                            _userExtension.ChangeActivationMode(!_users.IsActivated, _users.UserId);
                            lblSMessage.Text += _users.IsActivated
                                                    ? string.Format("{0} {1} deactivated <br/>", _users.FirstName,
                                                                    _users.LastName)
                                                    : string.Format("{0} {1} activated <br/>", _users.FirstName,
                                                                    _users.LastName);
                        }
                    }
                }
                catch (Exception exception)
                {
                    throw exception;
                }
            }
            grdUsers.DataBind();
        }

        protected void HyperLinkActivate_Click(object sender, EventArgs e)
        {
            var lnk = (LinkButton)sender;
            _userExtension = new UserExtension();
            _users = new Users(_userExtension.SelectUser("UserId", lnk.CommandArgument).Rows[0]);
            if (Page.User.Identity.Name.Equals(_users.Email, StringComparison.OrdinalIgnoreCase))
            {
                lblSMessage.Text = string.Format("<b>User {0} {1} is Administrator</b><br/>You cannot change activation mode of your own account<br/>",
                                                 _users.FirstName, _users.LastName);
                return;
            }
            _userExtension.ChangeActivationMode(!_users.IsActivated, _users.UserId);
            lblSMessage.Text = _users.IsActivated ? string.Format("{0} {1} deactivated <br/>", _users.FirstName,
                                                                    _users.LastName)
                                                    : string.Format("{0} {1} activated <br/>", _users.FirstName,
                                                                    _users.LastName);
            grdUsers.DataBind();

        }

        protected void btnDelete_Click(object sender, EventArgs e)
        {
            lblSMessage.Text = string.Empty;
            _userExtension = new UserExtension();
            foreach (GridViewRow row in grdUsers.Rows)
            {
                try
                {
                    var chkbx = (CheckBox)row.FindControl("chkSelect");
                    if (chkbx != null && chkbx.Checked)
                    {
                        _users = new Users(_userExtension.SelectUser("UserId", grdUsers.DataKeys[row.RowIndex].Value.ToString()).Rows[0]);
                        if (Page.User.Identity.Name.Equals(_users.Email, StringComparison.OrdinalIgnoreCase))
                        {
                            lblSMessage.Text =
                                string.Format(
                                    "<b>User {0} {1} is Administrator</b><br/>You cannot delete your own account<br/>",
                                    _users.FirstName, _users.LastName);
                        }
                        else
                        {
                            _userExtension.DeleteUser(int.Parse(row.Cells[1].Text));
                            lblSMessage.Text += string.Format("User {0} {1} - ({2}) Delete succeeded.<br/>",
                                                              _users.FirstName, _users.LastName, _users.UserType);
                        }
                    }
                }
                catch (Exception exception)
                {
                    throw exception;
                }
            }
            grdUsers.DataBind();
        }

        protected void btnSelectAll_Click(object sender, EventArgs e)
        {
            foreach (GridViewRow row in grdUsers.Rows)
            {
                var chkbx = (CheckBox)row.FindControl("chkSelect");
                if (chkbx == null) continue;
                chkbx.Checked = !chkbx.Checked;
                btnSelectAll.Text = chkbx.Checked ? "Deselect All" : "Select All";
            }
        }
    }
}
